package com.tang.Web;

import com.google.gson.Gson;
import com.tang.pojo.Book;
import com.tang.service.BookServiceImpl;
import com.tang.pojo.Cart;
import com.tang.pojo.CartItems;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class CartServlet extends BaseServlet{
    protected void addBookToCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        int idInt=Integer.parseInt(id);
        BookServiceImpl bsi=new BookServiceImpl();
        Book book=bsi.queryABook(idInt);
        CartItems ci=new CartItems();
        ci.setName(book.getName());
        ci.setPrice(book.getPrice());
        ci.setCount(ci.getCount());
        if(req.getSession().getAttribute("cart")==null) {
            Cart ct = new Cart();
            ct.addItem(ci);
            req.getSession().setAttribute("cart",ct);
        }else{
            Cart cart=(Cart)req.getSession().getAttribute("cart");
            cart.addItem(ci);
//            for(CartItems cit:cart.getItems()){
//                System.out.println(cit);
//            }
            req.getSession().setAttribute("cart",cart);
        }
        System.out.println("成功将"+ci.getName()+"商品加入购物车!");
        System.out.println(req.getHeader("referer"));
        resp.sendRedirect(req.getHeader("referer"));
    }
    protected void deleteBookToCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        Cart cart=(Cart)req.getSession().getAttribute("cart");
        int idInt=Integer.parseInt(id);
        cart.deleteItem(idInt);
        System.out.println("成功删除商品!");
        resp.sendRedirect(req.getHeader("referer"));
    }
    protected void clearCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart=(Cart)req.getSession().getAttribute("cart");
        cart.clearCart();
        System.out.println("成功清空购物车");
        resp.sendRedirect(req.getHeader("referer"));
    }
    protected void updateBookToCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        int idInt=Integer.parseInt(id);
        String count=req.getParameter("count");
        int countInt=Integer.parseInt(count);
        Cart cart=(Cart)req.getSession().getAttribute("cart");
        cart.updateCount(idInt,countInt);
        System.out.println("修改图书信息成功!");
        resp.sendRedirect(req.getHeader("referer"));
    }
    protected void AjaxaddBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        int idInt=Integer.parseInt(id);
        BookServiceImpl bsi=new BookServiceImpl();
        Book book=bsi.queryABook(idInt);
        CartItems ci=new CartItems();
        ci.setName(book.getName());
        ci.setPrice(book.getPrice());
        ci.setCount(ci.getCount());
        Cart cart = new Cart();
        if(req.getSession().getAttribute("cart")==null) {
            cart.addItem(ci);
            req.getSession().setAttribute("cart",cart);
        }else{
            cart=(Cart)req.getSession().getAttribute("cart");
            cart.addItem(ci);
            req.getSession().setAttribute("cart",cart);
        }
        System.out.println("成功将"+ci.getName()+"商品加入购物车!");
        HashMap<String,String> hm=new HashMap<String,String>();
        hm.put("count",cart.getCountTotal()+"");
        hm.put("name",ci.getName());
        Gson gson=new Gson();
        resp.getWriter().write(gson.toJson(hm));
    }
}
